package name.matthewgreet.example7.action; import org.apache.struts2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.interceptor.validation.SkipValidation; import com.opensymphony.xwork2.validator.annotations.Validations; import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator; import name.matthewgreet.example7.form.ArithmeticForm; import name.matthewgreet.example7.form.DummyForm; /** * Tests Struts 2 automatic type conversion for integers. */ @InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true"}) @Results({ @Result(name=ActionSupport.ERROR, location="/pages/ArithmeticSeparateFormWithOtherAndValidations.jsp"), @Result(name=ActionSupport.INPUT, location="/pages/ArithmeticSeparateFormWithOtherAndValidations.jsp"), @Result(name=ActionSupport.SUCCESS, location="/pages/ArithmeticSeparateFormWithOtherAndValidations.jsp") }) public class ArithmeticSeparateFormWithOtherAndValidationsAction extends ActionSupport { private static final long serialVersionUID = -6084392315736424463L; private ArithmeticForm form; private DummyForm dummyForm; private String resultText; @Action("/ArithmeticSeparateFormWithOtherAndValidations_dummy") @Validations( visitorFields = { @VisitorFieldValidator(fieldName = "dummyForm") } ) public String dummy() throws Exception { return SUCCESS; } @Action("/ArithmeticSeparateFormWithOtherAndValidations") @Override @SkipValidation public String execute() throws Exception { try { int result; if (form == null) { resultText = ""; } else if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() * form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } @Action("/ArithmeticSeparateFormWithOtherAndValidations_submit") @Validations( visitorFields = { @VisitorFieldValidator(fieldName = "form") } ) public String submit() throws Exception { try { int result; if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() + form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } public ArithmeticForm getForm() { if (form == null) { form = new ArithmeticForm(); } return form; } public DummyForm getDummyForm() { if (dummyForm == null) { dummyForm = new DummyForm(); } return dummyForm; } public String getResultText() { return resultText; } }